import axios from 'axios'

const state = {
  events: [],
  temp: [],
  skip: 0,
  eventItem: {}
}

const mutations = {
  loadMore (state, payload) {
    state.skip += 3
    state.events = state.events.concat(payload.res)
  },
  getSingleEvent (state, payload) {
    state.eventItem = payload.res
  },
  getMessage(state,payload){
  	state.skip += 3
  	state.events = state.events.concat(payload.res)
  }
}

const actions = {
  /**
   * Loading more data
   * skip: 3 default
   * count: 3 default
   */ 
  loadMore ({commit, state}) {
    axios
      .get('https://api.douban.com/v2/event/list?loc=108288&start=' +
        state.skip + '&count=3')
      .then(function(response){
          commit({
            type: 'loadMore',
            res: res.body.events
          });
          console.log(response)
      })
      .catch(function(error){
      	console.log(error)
      })
      
  },
    getMessage({commit,state}){
    	axios({
    		method:'get',
    		url:'/api'+'v2/event/list?loc=108288&start='+state.skip+'&count=3'
    	})
      .then(function(response){
          commit({
          	type:'getMessage',
          	res:response.data.events
          });
          console.log(response.data)
      })
      .catch(function(error){
      	console.log(error)
      })
    },
  /**
   * Getting single event
   * id: event id
   */
  getSingleEvent ({commit, state}, payload) {
    axios({
    		method:'get',
    		url:'/api'+'v2/event/'+payload.id,
    		headers: {
    		'Content-Type': 'application/x-www-form-urlencoded',
  	}
    	})
      .then(function(response){
          commit({
          	type:'getSingleEvent',
          	res:response.data
          });
          console.log(response.data)
          console.log('/api'+'v2/event/'+payload.id)
      })
      .catch(function(error){
      	console.log(error)
      })
  }
}

export default {
  state,
  mutations,
  actions
}



